package com.scaffold.authority.service;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import wangjg.commons.page.Page;

import com.scaffold.authority.dao.AuthorityDAO;
import com.scaffold.authority.dao.RoleDAO;
import com.scaffold.authority.dao.UserAuthorityRelationDAO;
import com.scaffold.authority.dao.UserDAO;
import com.scaffold.authority.dao.UserRoleRelationDAO;
import com.scaffold.authority.entity.Authority;
import com.scaffold.authority.entity.Role;
import com.scaffold.authority.entity.User;
import com.scaffold.authority.entity.UserAuthorityRelation;
import com.scaffold.authority.entity.UserRoleRelation;

@Service
@Transactional
public class UserService{

	@Autowired
	private UserDAO userDAO;
	@Autowired
	private UserRoleRelationDAO userRoleRelationDAO;
	@Autowired
	private RoleDAO roleDAO;
	@Autowired
	private UserAuthorityRelationDAO userAuthorityRelationDAO;
	@Autowired
	private AuthorityDAO authorityDAO;

	public User getUser(Long id ) {
		return this.userDAO.findById(id);
	}

	public User findByName(String username) {
		return this.userDAO.findByProperty("username", username);
	}
	
	public void save(User user){
		Timestamp now = new Timestamp(System.currentTimeMillis());
		if(user.getId()==null){
			user.setCreated(now);
			user.setModified(now);
			this.userDAO.save(user);
		}else{
			user.setModified(now);
			this.userDAO.update(user);
		}
	}

	public void update(User user){
		Timestamp now = new Timestamp(System.currentTimeMillis());
		user.setModified(now);
		this.userDAO.update(user);
	}

	public void delete(User user){
		this.userDAO.delete(user);
		this.userRoleRelationDAO.deleteByProperty("userId", user.getId());
		this.userAuthorityRelationDAO.deleteByProperty("userId", user.getId());
	}

	public void batchDelete(String idseq) {
		String[] sa = idseq.split(",");
		for(String s:sa){
			Long id = Long.parseLong(s);
			this.userDAO.delete(id);
			this.userRoleRelationDAO.deleteByProperty("userId", id);
			this.userAuthorityRelationDAO.deleteByProperty("userId", id);
		}
	}

	public List<User> pageQuery(Page page, String username, String password, String keyword) {
		return this.userDAO.pageQuery(page, username, password, keyword);
	}

	public void saveRoles(Long userId, String roleIds) {
		userRoleRelationDAO.deleteByProperty("userId", userId);
		
		if(userId==null || roleIds==null || roleIds.isEmpty()){
			return;
		}
		String[] idseq = roleIds.split(",");
		Timestamp now = new Timestamp(System.currentTimeMillis());
		for(String s:idseq){
			Long roleId = Long.parseLong(s);
			UserRoleRelation item = new UserRoleRelation();
			item.setUserId(userId);
			item.setRoleId(roleId);
			item.setCreated(now);
			item.setModified(now);
			userRoleRelationDAO.save(item);
		}
	}

	public List<Long> getRoleIdList(Long userId) {
		return userRoleRelationDAO.findPropertyList("roleId", "userId", userId);
	}

	public List<Role> getRoleList(Long userId) {
		List idList =  userRoleRelationDAO.findPropertyList("roleId", "userId", userId);
		return roleDAO.findByIdList(idList);
	}

	public void saveAuthoritys(Long userId, String authorityIds) {
		userAuthorityRelationDAO.deleteByProperty("userId", userId);
		
		if(userId==null || authorityIds==null || authorityIds.isEmpty()){
			return;
		}
		String[] idseq = authorityIds.split(",");
		Timestamp now = new Timestamp(System.currentTimeMillis());
		for(String s:idseq){
			Long authorityId = Long.parseLong(s);
			UserAuthorityRelation item = new UserAuthorityRelation();
			item.setUserId(userId);
			item.setAuthorityId(authorityId);
			item.setCreated(now);
			item.setModified(now);
			userAuthorityRelationDAO.save(item);
		}
	}

	public List<Long> getAuthorityIdList(Long userId) {
		return userAuthorityRelationDAO.findPropertyList("authorityId", "userId", userId);
	}

	public List<Authority> getAuthorityList(Long userId) {
		List<Long> idList =  userAuthorityRelationDAO.findPropertyList("authorityId", "userId", userId);
		return authorityDAO.findByIdList(idList);
	}

	public User getUserWithRoles(Long id ) {
		User user = this.userDAO.findById(id);
		List<Role> roleList = this.getRoleList(id);
		user.setRoles(roleList);
		return user;
	}
	public User getUserWithAuthoritys(Long id ) {
		User user = this.userDAO.findById(id);
		List<Authority> authList = this.getAuthorityList(id);
		user.setAuths(authList);
		return user;
	}
}
